<?php
class Admin_ChoixController extends My_Controller_Admin_Action
{
	protected $choixModel = '';

	public function _init()
	{
		$this->choixModel = new Admin_Model_Choix();
	}

	public function indexAction()
	{
	    $_SESSION['dtvente'] =  '';
	    $ventesModel = new Admin_Model_Ventes();
	    $rows = $ventesModel->getListVentes();
	    $this->view->rows = $rows;

	    $vente = $this->_getParam('num_vente', '');
	    $this->view->vente = $vente;
	}

	public function menuAction()
	{
	    $numVente = $this->_getParam('num_vente');
	    $this->_redirect('admin/clients/?num_vente=' . $numVente);
	}

	public function usersAction()
	{
	    $vente = $this->_getParam('num_vente', '');
	    $this->view->num_vente = $vente;

	    $num_ventes = $this->_getParam('num_ventes');
	    $users = $this->choixModel->getListUser();
	    $this->view->users = $users;
	}

    public function addAdminAction()
	{
	    $num_ventes = $this->_getParam('vente');
	    $this->view->num_ventes = $num_ventes;
		echo "userventes";
	}

	public function saveVentesUserAction()
	{
	    $post = $this->getRequest()->getPost();
	    $num_ventes = $this->_getParam('num_ventes');
		$lalimite="";
		if (isset($post[limited])) {
			foreach ($post[limited] as $choix) {
				$lalimite = $choix .",".$lalimite;
			}
		}
		$lalimite = substr($lalimite,0,-1);

		$data = array(
		    'nom'	    => addslashes($post['nom']),
		    'prenom'	=> addslashes($post['prenom']),
    		'email'	    => addslashes($post['email']),
    		'userid'	=> $post['userid'],
    		'password'	=> md5($post['password']),
    		'limited'	=> $lalimite
		);

		$this->ventesModel->insertVentesUser($data);
		$this->_redirect('admin/ventes/choose-ventes/vente/' . $num_ventes);
	}

	public function updateAdminAction() {
	/*
		$_POST[nom] = addslashes($_POST[nom]);
		$_POST[prenom] = addslashes($_POST[prenom]);
		if ($_POST[password] == "") {
			$_POST[password] = $_POST[passwordORG];
		} else {
			$_POST[password] = md5($_POST[password]);
		}
		if ($_SESSION[limite] == "") {
			$lalimite="";
			if (isset($_POST[limited])) {
				foreach ($_POST[limited] as $choix) {
					$lalimite = $choix .",".$lalimite;
				}
			}
				$lalimite = substr($lalimite,0,-1);
		} else {
			$lalimite = $_SESSION[limite];
		}
		$q = "update admin set nom = '$_POST[nom]', prenom = '$_POST[prenom]', email = '$_POST[email]', userid = '$_POST[userid]', password = '$_POST[password]', limited = '$lalimite' where p_admin = $_GET[num]";
		$r = mysql_query($q) or die ("erreur update : $q<br>".mysql_error());
		echo "<script language=\"javascript\">";
		echo "window.location.replace(\"./admin.php?numVente=$_GET[numVente]\")";
		echo "</script>";
	*/
	}

	public function renumAction()
	{
	    $num_ventes = $this->_getParam('num_vente');
	    $this->view->num_vente = $num_ventes;
	}
}

